package com.aimosheng.common;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aimosheng.common.bean.ChatAnchorParam;
import com.aimosheng.common.bean.ChatAudienceParam;
import com.aimosheng.common.http.CommonHttpUtil;
import com.aimosheng.common.utils.L;
import com.aimosheng.common.utils.RouteUtil;
import com.aimosheng.common.utils.SpUtil;


/**
 * Created by cxf on 2017/8/3.
 */

public class CommonAppContext extends MultiDexApplication {

    public static CommonAppContext sInstance;
    private int mCount;
    private boolean mFront;//是否前台

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        //初始化Http
        CommonHttpUtil.init();
        //初始化友盟统计
//        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
        registerActivityLifecycleCallbacks();
    }

    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }

    private void registerActivityLifecycleCallbacks() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                mCount++;
                if (!mFront) {
                    mFront = true;
                    L.e("AppContext------->处于前台");
                    CommonAppConfig.getInstance().setFrontGround(true);
                    String chatCallAncData = SpUtil.getInstance().getStringValue(SpUtil.CHAT_CALL_ANC_DATA);
                    if(!TextUtils.isEmpty(chatCallAncData)){
                        JSONObject obj= JSON.parseObject(chatCallAncData);
                        ChatAnchorParam param = new ChatAnchorParam();
                        param.setSessionId(obj.getString("showid"));
                        param.setChatType(obj.getIntValue("type"));
                        param.setAudienceID(obj.getString("id"));
                        param.setAudienceAvatar(obj.getString("avatar"));
                        param.setAudienceName(obj.getString("user_nickname"));
                        param.setAnchorActive(false);
                        if(CommonAppConfig.getInstance().isLaunched()){
                            RouteUtil.forwardAnchorActivity(param);
                        }
                        return;
                    }

                    String chatCallAudData = SpUtil.getInstance().getStringValue(SpUtil.CHAT_CALL_AUD_DATA);
                    if(!TextUtils.isEmpty(chatCallAudData)){
                        JSONObject obj= JSON.parseObject(chatCallAudData);
                        ChatAudienceParam param = new ChatAudienceParam();
                        param.setSessionId(obj.getString("showid"));
                        param.setChatType(obj.getIntValue("type"));
                        param.setAnchorID(obj.getString("id"));
                        param.setAnchorAvatar(obj.getString("avatar"));
                        param.setAnchorName(obj.getString("user_nickname"));
                        param.setAnchorLevel(obj.getIntValue("level_anchor"));
                        param.setAnchorPrice(obj.getString("total"));
                        param.setAudienceActive(false);
                        if(CommonAppConfig.getInstance().isLaunched()){
                            RouteUtil.forwardAudienceActivity(param);
                        }
                    }
                }
            }

            @Override
            public void onActivityPaused(Activity activity) {
                mCount--;
                if (mCount == 0) {
                    mFront = false;
                    L.e("AppContext------->处于后台");
                    CommonAppConfig.getInstance().setFrontGround(false);
                }
            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

}
